package com.remeberword;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.remeberword.db.Word;

import java.util.ArrayList;
import java.util.List;

public class WordManageActivity extends AppCompatActivity {

    private List<Word> wordList = new ArrayList<>();
    private RecyclerView recyclerView;
    private ManageWordAdapter adapter;
    private DrawerLayout mDrawerLayout;
    private LinearLayoutManager layoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        NavigationView navView = (NavigationView) findViewById(R.id.nav_view);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);
        }
        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.show_word:
                        startActivity(new Intent(WordManageActivity.this, MainActivity.class));
                        break;
                    case R.id.word_add:
                        startActivity(new Intent(WordManageActivity.this, AddWordActivity.class));
                        break;
                    case R.id.backup:
                        startActivity(new Intent(WordManageActivity.this, BackupRestoreActivity.class));
                        break;
                }
                mDrawerLayout.closeDrawers();
                return true;
            }
        });
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new ManageWordAdapter(wordList, this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("单词管理");
        initWord();
        adapter.notifyDataSetChanged();

        SharedPreferences pref = getSharedPreferences("main", MODE_PRIVATE);
        int leavePosition = pref.getInt("leavePosition", 0);
        int offset = pref.getInt("offset", 0);
        layoutManager.scrollToPositionWithOffset(leavePosition,offset);
    }

    private void initWord() {
        wordList.clear();
        MyDatabaseHelper dbHelper = new MyDatabaseHelper(this, "WordPair.db", null);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from wordPair order by frequency desc, english", new String[]{});
//        Cursor cursor = db.query("wordPair", null, null, null, null, null, "frequency");
        if (cursor.moveToFirst()) {
            do {
                String english = cursor.getString(cursor.getColumnIndex("english"));
                String chinese = cursor.getString(cursor.getColumnIndex("chinese"));
                int frequency = cursor.getInt(cursor.getColumnIndex("frequency"));
                wordList.add(new Word(english, chinese, frequency));
            } while (cursor.moveToNext());
        }
        cursor.close();
    }

    public void loadDetailActivity(Word word) {
        Intent intent = new Intent(WordManageActivity.this, WordPairDetailActivity.class);
        String str = word.getEnglish() + "&" + word.getChinese() + "&" + word.getFrequency();
        intent.putExtra("wordPiar", str);
        startActivity(intent);
    }

//    @Override
//    protected void onPause() {
//        super.onPause();
//
//        int offset = 0, leavePosition = layoutManager.findFirstVisibleItemPosition();
//
//        View currentView = layoutManager.findViewByPosition(leavePosition);
//        if(currentView!=null)
//            offset = currentView.getTop();
//
//        SharedPreferences.Editor editor = getSharedPreferences("word", MODE_PRIVATE).edit();
//        editor.putInt("leavePosition", leavePosition);
//        editor.putInt("offset", offset);
//        editor.apply();
//    }
}
